-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
63 lines (51 loc) · 1.8 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
public class Dijkstra {
static int findMinDistance(int[] dist, boolean[] visited, int n) {
int min = Integer.MAX_VALUE, minIndex = -1;
for (int v = 0; v < n; v++) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}
static void dijkstra(int[][] graph, int n, int start) {
int[] dist = new int[n];
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
dist[i] = Integer.MAX_VALUE;
visited[i] = false;
}
dist[start] = 0;
for (int count = 0; count < n - 1; count++) {
int u = findMinDistance(dist, visited, n);
visited[u] = true;
for (int v = 0; v < n; v++) {
if (!visited[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
System.out.println("Vertex\tDistance from Source");
for (int i = 0; i < n; i++) {
System.out.println(i + "\t" + dist[i]);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of vertices: ");
int n = scanner.nextInt();
int[][] graph = new int[n][n];
System.out.println("Enter the adjacency matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j] = scanner.nextInt();
}
}
System.out.print("Enter the starting vertex: ");
int start = scanner.nextInt();
dijkstra(graph, n, start);
scanner.close();
}
}